package com.colorlight.cloud.complete.entity.instructions;

import com.colorlight.cloud.common.exception.ParameterException;
import com.colorlight.cloud.common.model.ExceptionEnum;
import com.colorlight.cloud.complete.api.CompleteEntityScheduleActions;
import com.colorlight.cloud.complete.entity.enumerate.ScheduleResultStruct;
import com.colorlight.cloud.complete.entity.po.ScreenCommandSchedule;
import com.colorlight.iot.proto.complete.instruction.CompleteInstruction;
import com.google.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.io.Serializable;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 设置高级控制
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Slf4j
public class SetCommandSchedule implements CompleteEntityScheduleActions {

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @Builder
    public static class InputMsg implements Serializable {
        /**
         * 屏幕命令计划
         */
        private List<ScreenCommandSchedule> screenCommandSchedule;
    }

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @Builder
    public static class OutputMsg implements Serializable {
        /**
         * 计划结果结构
         */
        private ScheduleResultStruct scheduleResultStruct;
    }

    private InputMsg inputMsg;
    private OutputMsg outputMsg;

    @Override
    public ByteString objectConvertBytes() {
        final CompleteInstruction.SetCommandSchedule.InputMsg.Builder inputMsg =
                CompleteInstruction.SetCommandSchedule.InputMsg.newBuilder();
        List<ScreenCommandSchedule> screenCommandSchedule = this.getInputMsg().getScreenCommandSchedule();
        if (Objects.nonNull(screenCommandSchedule)) {
            inputMsg.addAllScreenCommandSchedule(
                    screenCommandSchedule
                            .stream()
                            .map(ScreenCommandSchedule::toProto)
                            .collect(Collectors.toList()));
        }
        final CompleteInstruction.SetCommandSchedule.Builder builder =
                CompleteInstruction.SetCommandSchedule.newBuilder();
        builder.setInput(inputMsg);
        return builder.build().toByteString();
    }

    @Override
    public SetCommandSchedule byteConvertObject(ByteString bytes) {
        CompleteInstruction.SetCommandSchedule setCommandSchedule = null;
        try {
            setCommandSchedule = CompleteInstruction.SetCommandSchedule.parseFrom(bytes);
        } catch (InvalidProtocolBufferException e) {
            log.error("", e);
        }
        if (Objects.isNull(setCommandSchedule)) {
            throw new ParameterException(ExceptionEnum.MSG_PARAMETER_EXCEPTION);
        }
        SetCommandSchedule.OutputMsg outputMsg = new SetCommandSchedule.OutputMsg();
        CompleteInstruction.SetCommandSchedule.OutputMsg output = setCommandSchedule.getOutput();
        ScheduleResultStruct scheduleResultStruct = ScheduleResultStruct.getScheduleResultStruct(output.getScheduleResultStruct());
        outputMsg.setScheduleResultStruct(scheduleResultStruct);
        this.setOutputMsg(outputMsg);
        return this;
    }

}

